package com.hoshiicloud.purchase.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.purchase.Podetail;
import com.hoshiicloud.purchase.param.PodetailAddParam;
import com.hoshiicloud.purchase.param.PodetailQueryParam;
import com.hoshiicloud.purchase.param.PodetailUpdateParam;
import com.hoshiicloud.purchase.service.PodetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Slf4j
@RestController
@RequestMapping("/podetail")
@Api(value = "采购单详情", tags = {"采购单详情"})
public class PodetailController {

    @Autowired
    private PodetailService podetailService;

    /**
     * @Author:campchen
     * @Description:
     * @Date:上午 10:25 2019/6/14
     * @Param:
     * @return:
     */
    @ApiOperation(value = "采购主单分页查询")
    @PostMapping("/getPage")
    public Response<IPage<Podetail>> getPage(@RequestBody PodetailQueryParam param) {
        try {
            QueryWrapper<Podetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getPoId() != null), Podetail::getPoId, param.getPoId())
                    .eq(StringUtils.isNotBlank(param.getSpuName()),Podetail::getSpuName, param.getSpuName())
                    .eq(StringUtils.isNotBlank(param.getSkuName()), Podetail::getSkuName, param.getSkuName())
                    .eq(StringUtils.isNotBlank(param.getSpuNo()), Podetail::getSpuNo, param.getSpuNo())
                    .eq((param.getSpuId() != null), Podetail::getSpuId, param.getSpuId())
                    .eq(StringUtils.isNotBlank(param.getSkuNo()), Podetail::getSkuNo, param.getSkuNo())
                    .eq((param.getSkuId() != null), Podetail::getSkuId, param.getSkuId())
                    .eq((param.getRate() != null), Podetail::getRate, param.getRate())
                    .eq((param.getRatefee() != null), Podetail::getRatefee, param.getRatefee())
                    .eq((param.getAmount() != null), Podetail::getAmount, param.getAmount())
                    .eq(StringUtils.isNotBlank(param.getUnit()),Podetail::getUnit, param.getUnit())
                    .eq(StringUtils.isNotBlank(param.getSku()), Podetail::getSku, param.getSku())
                    .eq((param.getUnitPrice() != null), Podetail::getUnitPrice, param.getUnitPrice())
                    .eq((param.getQuantity() != null), Podetail::getQuantity, param.getQuantity())
                    .eq((param.getStatus() != null), Podetail::getStatus, param.getStatus())
                    .eq((param.getShippongCost() != null),Podetail:: getShippongCost, param.getShippongCost())
                    .eq((param.getOtherCost() != null), Podetail::getOtherCost,param.getOtherCost());
            IPage<Podetail> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
            return Response.data(podetailService.getPage(page,queryWrapper,key));
        } catch (NumberFormatException e) {
            log.error("根据供应商与采购单状态进行分页查询异常",e);
            return Response.failed("根据供应商与采购单状态进行分页查询异常");
        }

    }


    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(podetailService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public Response save(@RequestBody @Valid PodetailAddParam param) {
        Podetail entity;
        try {
            entity = BeanUtils.covertType(param, Podetail.class);
        } catch (Exception e) {
            log.error("新增Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(podetailService.save(entity));

    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public Response update(@RequestBody @Valid PodetailUpdateParam param) {
        Podetail entity;
        try {
            entity = BeanUtils.covertType(param, Podetail.class);
        } catch (Exception e) {
            log.error("采购-管理-修改Bean转换异常", e);
            return Response.failed("采购-管理-修改Bean转换异常");
        }
        return Response.data(podetailService.updateById(entity));
    }



}
